﻿using MVCManage.Model;
using MVCManage.Model.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MVCMamage.DAL;
namespace MVCManage.BLL
{
    public class GoodsBLL : BaseBLL<Goods>
    {
        public override void SetDal()
        {
            Dal = new GoodsDAL();
        }

        public List<Goods> GetGoodsPageList(string name,int page,int size,out int count) {
            var zhi = Dal.Query();
            var list = zhi;
            if (!string.IsNullOrWhiteSpace(name)) {
                list = zhi.Where(n => n.name.Contains(name) || n.content.Contains(name));
            }
            count = list.Count();
            var pagelist = zhi.OrderBy(n => n.gId).Skip((page - 1) * size).Take(size);
            return pagelist.ToList();
        }

        public bool UpdateGood(Goods good) {
            var data = base.Query().Where(n => n.gId == good.gId).FirstOrDefault();
            data.name = good.name;
            data.price = good.price;
            data.unit = good.unit;
            data.gtId = good.gtId;
            data.use_property = good.use_property;
            data.use_spec = false;
            data.content = good.content;
            data.images = good.images;
            data.sort = good.sort;
            var keyDal = new GoodsPropertyKeyDal();
            var valueDal = new GoodsPropertyValueDal();
            foreach (var key in data.GoodsPropertyKey)
            {
               valueDal.Deletes(key.GoodsPropertyValue.ToList());
            }
            keyDal.Deletes(data.GoodsPropertyKey.ToList());
            keyDal.Add(good.GoodsPropertyKey.ToList());
            Dal.Update(data);
            return Dal.SaveChanges();

        }

        public bool DeleteGood(int id) {
            var data = base.Query().Where(n => n.gId == id).FirstOrDefault();
            var keyDal = new GoodsPropertyKeyDal();
            var valueDal = new GoodsPropertyValueDal();
            foreach (var key in data.GoodsPropertyKey)
            {
                valueDal.Deletes(key.GoodsPropertyValue.ToList());
            }
            keyDal.Deletes(data.GoodsPropertyKey.ToList());
            Dal.Delete(data);
            return Dal.SaveChanges();
        }

        public bool DeleteGoods(int[] ids) {
            var data = base.Query().Where(n => ids.Contains(n.gId));
            var keyDal = new GoodsPropertyKeyDal();
            var valueDal = new GoodsPropertyValueDal();
            foreach (var good in data)
            {
                foreach (var key in good.GoodsPropertyKey)
                {
                    valueDal.Deletes(key.GoodsPropertyValue.ToList());
                }

            }
            foreach (var good in data)
            {
                keyDal.Deletes(good.GoodsPropertyKey.ToList());
            }
            Dal.Deletes(data.ToList());
            return Dal.SaveChanges();
          

        }

        public bool ShangJia(int gid) {
            var zhi = Dal.Query().Where(n => n.gId == gid).FirstOrDefault();
            zhi.on_sale = true;
            Dal.Update(zhi);
            return Dal.SaveChanges();
        }
        public bool XiaJia(int gid)
        {
            var zhi = Dal.Query().Where(n => n.gId == gid).FirstOrDefault();
            zhi.on_sale = false;
            Dal.Update(zhi);
            return Dal.SaveChanges();
        }

    }
}
